package com.xxxt.zpc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.xxxt.entity.Feedback;
import com.xxxt.entity.Inform;
import com.xxxt.entity.Student;
import com.xxxt.zpc.service.FeedbackService;
import com.xxxt.zpc.service.InformService;

/**
 * 老师查看反馈
 * 
 * @author 星梦
 *
 */
@RequestMapping("feedback")
@Controller
public class FeedbackController {

	@Autowired
	private HttpSession session;
	@Autowired
	private InformService informService;
	@Autowired
	private FeedbackService feedbackService;

	/*
	 * 老师查看自己发布的通知，并且紧跟反馈
	 */
	@PostMapping("selectFeedback")
	@ResponseBody
	public JSONObject selectFeedback() {
		JSONObject json = new JSONObject();
		int status = 200;
		String message = "网络正在快马加鞭...";

		String id = (String) session.getAttribute("account");
		List<Inform> inform = informService.selectInformByTno(id);
		if (inform.size()<1) {
			status = -1;
			message = "您还没有发布通知";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		int number=feedbackService.selectStudentConut(id);//该老师带学生的人数
		List<Integer> readNumbers=new ArrayList<>();//未读人数的集合
		int readNumber=0;
		for(int i=0;i<inform.size();i++){
			readNumber=feedbackService.selectById(inform.get(i).getInformId());//已读人数
			readNumbers.add(i,number-readNumber);
		}
		json.put("readNumbers", readNumbers);
		json.put("list", inform);
		json.put("status", status);
		json.put("message", message);
		return json;
	}
	/*
	 * 老师查看某条通知的未读人数
	 */
	@PostMapping("selectFeedbackRead")
	@ResponseBody
	public JSONObject selectFeedbackById(String informId) {
		JSONObject json = new JSONObject();
		int status = 200;
		String message = "网络正在快马加鞭...";

		String id = (String) session.getAttribute("account");
		List<Feedback> list=feedbackService.selectFeedbckById(informId);//得到已读学生的信息
		List<Student> liststudent=feedbackService.selectStudent(id);
		String listId;
		String studentid;
		for(int i=0;i<list.size();i++){
			listId=list.get(i).getStudent().getSno();
			for(int j=0;j<liststudent.size();j++){
				studentid=liststudent.get(j).getSno();
				if(studentid.equals(listId)){
					liststudent.remove(j);
					
				}
			}
		}
		
		json.put("list", liststudent);
		json.put("status", status);
		json.put("message", message);
		return json;
	}
}
